"""平均分为4份"""
import json
import math

# 读取原始 JSON 文件
with open("aug_poems.json", "r", encoding="utf-8") as f:
    data = json.load(f)

# 将数据按“序号”排序（转为整数以确保排序正确）
sorted_items = sorted(data.items(), key=lambda x: int(x[1]["序号"]))

# 平均分成 4 份
num_splits = 4
total = len(sorted_items)
chunk_size = math.ceil(total / num_splits)

for i in range(num_splits):
    # 取出对应切片
    chunk_items = sorted_items[i * chunk_size : (i + 1) * chunk_size]
    # 转回 dict 格式
    chunk_dict = {k: v for k, v in chunk_items}
    # 写入文件
    with open(f"split_{i}.json", "w", encoding="utf-8") as f:
        json.dump(chunk_dict, f, ensure_ascii=False, indent=2)

    print(f"✅ 已写入 split_{i}.json，共 {len(chunk_dict)} 条")
